<?php

include_once("includes/Sigma.php");

$serverdb = "sussanboutique.no-ip.org";
$db = "erpsussan";
$userdb = "erpsussan";
$passdb = "loboferoz99";

$link = @mysql_connect($serverdb, $userdb, $passdb);
$result = @mysql_select_db($db, $link);

$tpl = new HTML_Template_Sigma( "templates" ,"cache");
$tpl->loadTemplatefile("analisis_stock.html", true, true);

$ptc = $_GET['ptc'];

$sql_descripcion = "SELECT * FROM producto_talle_color ptc JOIN productos p on ptc.codsussan = p.codsussan WHERE ptc.id = '".$ptc."'";

$result = mysql_query($sql_descripcion, $link);
$f = mysql_fetch_array($result);

$tpl->setVariable('codsussan', $f['codsussan']);
$tpl->setVariable('descri', $f['descri']);
$tpl->setVariable('talle', $f['talle']);
$tpl->setVariable('color', $f['color']);



//STOCK ACTUAL
$query = "SELECT s1.ptc, s1.stock AS sucursal1, s2.stock AS sucursal2
FROM (

SELECT *
FROM stock
WHERE sucursal =1
)s1
JOIN (

SELECT *
FROM stock
WHERE sucursal =2
)s2 ON ( s1.ptc = s2.ptc )
WHERE s1.ptc = '".$ptc."'";

$query = "SELECT stock 
FROM stock
WHERE sucursal = 1 AND ptc = '".$ptc."'";

$query2 = "SELECT stock 
FROM stock
WHERE sucursal = 2 AND ptc = '".$ptc."'";

$result = mysql_query($query, $link);
$result2 = mysql_query($query2, $link);

$f = mysql_fetch_array($result);
$f2 = mysql_fetch_array($result2);

$tpl->setVariable('ptc_a', $ptc);
$tpl->setVariable('sucursal1', $f[stock]);
$tpl->setVariable('sucursal2', $f2[stock]);


//COMPRAS
$query = "SELECT *
FROM (
`compras` c
JOIN compras_items ci ON c.idfactura = ci.idfactura
)
JOIN producto_talle_color ptc ON ci.codsussan = ptc.codsussan
AND ci.talle like ptc.talle
AND ci.color like ptc.color 
WHERE ptc.id like '".$ptc."'";

$result = mysql_query($query, $link);

while($f = mysql_fetch_array($result)){
	$tpl->setVariable('ptc', $ptc);
	$tpl->setVariable('cantidad', $f[cantidad]);
	$tpl->setVariable('fecha', $f[fechaingreso]);
	$tpl->parse("compras");
}

//VENTAS
$query = "SELECT *
FROM `ventas` v
JOIN ventastieneproductos vp ON v.idventa = vp.idticket
WHERE vp.ptc like '".$ptc."'" ;
$result_sf = mysql_query($query, $link);

echo $query;

/*
mysql_close($link);

$serverdb = "200.42.64.225";
$db = "erpsussan";
$userdb = "erpsussan";
$passdb = "loboferoz99";

$link3 = mysql_connect($serverdb, $userdb, $passdb);
mysql_select_db($db, $link3);
*/

while($f = mysql_fetch_array($result_sf)){
	$tpl->setVariable('ptc', $ptc);
	$tpl->setVariable('cantidad', $f[cantidad]);
	$tpl->setVariable('fecha', $f[fecha]);
	$tpl->setVariable('sucursal', $f[sucursal]);
	$tpl->setVariable('devuelto', $f[devuelto]);
	$tpl->parse("ventas");
}

/*
$result_parana = mysql_query($query, $link3);

while($f = mysql_fetch_array($result_parana)){
	$tpl->setVariable('ptc', $ptc);
	$tpl->setVariable('cantidad', $f[cantidad]);
	$tpl->setVariable('fecha', $f[fecha]);
	$tpl->setVariable('sucursal', $f[sucursal]);
	$tpl->setVariable('devuelto', $f[devuelto]);
	$tpl->parse("ventas");
}

*/

//TRANSFERENCIAS
mysql_close();
$serverdb = "sussanboutique.no-ip.org";
$db = "erpsussan";
$userdb = "erpsussan";
$passdb = "loboferoz99";

$link = @mysql_connect($serverdb, $userdb, $passdb);
$result = @mysql_select_db($db, $link);

$query = "SELECT *
FROM `transferencias` t
JOIN transferenciastieneproductos tp ON ( t.id = tp.idtransferencia )
WHERE tp.ptc = '".$ptc."'";
$result = mysql_query($query, $link);

while($f = mysql_fetch_array($result)){
	$tpl->setVariable('ptc', $ptc);
	$tpl->setVariable('cantidad', $f[cant]);
	$tpl->setVariable('fecha', $f[fecha]);
	$tpl->setVariable('origen', $f[idsucorigen]);
	$tpl->setVariable('destino', $f[idsucdestino]);
	$tpl->setVariable('idtransferencia', $f[idtransferencia]);
	$tpl->parse("transferencias");
}

//CAMBIOS DE STOCK (HAY QUE SACARLO DE LA BASE DE DATOS DE SINCRONIZACION)

mysql_close($link);

$serverdb = "sussanboutique.no-ip.org";
$db = "sincronizar";
$userdb = "sincronizar";
$passdb = "loboferoz99";

$link2 = mysql_connect($serverdb, $userdb, $passdb);
$result = mysql_select_db($db, $link2);
$query = "
SELECT *
FROM cambios_servidor
WHERE tabla = 'stock'
AND cambio like '%".$ptc."%'";

$result = mysql_query($query, $link2);

while($f = mysql_fetch_array($result)){
	$tpl->setVariable('ptc', $ptc);
	$tpl->setVariable('cambio', $f[cambio]);
	$tpl->setVariable('fecha', $f[fecha]);
	$tpl->setVariable('version', $f[version]);
	$tpl->setVariable('sucursal', $f[sucursal]);
	$tpl->parse("stock");
}



//CAMBIOS DE TRANSFERENCIASTIENEPRODUCTOS(HAY QUE SACARLO DE LA BASE DE DATOS DE SINCRONIZACION)

$query = "
SELECT *
FROM cambios_servidor
WHERE tabla = 'transferenciastieneproductos'
AND cambio LIKE '%".$ptc."%'";

$result = mysql_query($query, $link2);

while($f = mysql_fetch_array($result)){
	$tpl->setVariable('ptc', $ptc);
	$tpl->setVariable('cambio', $f[cambio]);
	$tpl->setVariable('fecha', $f[fecha]);
	$tpl->setVariable('version', $f[version]);
	$tpl->setVariable('sucursal', $f[sucursal]);
	$tpl->parse("transferenciastieneproductos");
}



$tpl->show();


?>